﻿
using System;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace the_shapes
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Random random = new Random();
            Shape[] shape = new Shape[10];
            double totalarea = 0.0;
            for (int i = 0; i < shape.Length; i++)
            {
                int shapeType = random.Next(0, 44);

                switch (shapeType)
                {
                    case 0:
                        double width = random.NextDouble();
                        double height = random.NextDouble();
                        shape[i] = new Rectangle(width, height);
                        break;
                    case 1:
                        double radius = random.NextDouble();
                        shape[i] = new Circle(radius);
                        break;
                    case 2:
                        double side = random.NextDouble();
                        shape[i] = new Square(side);
                        break;
                }

                if (shape[i].Isvalid())
                {
                    totalarea += shape[i].calculatearea();
                }
                else
                {
                    Console.WriteLine("生成不合法的形状，skip");
                }
            }
            Console.WriteLine($"10个形状的总面积为: {totalarea}");
            Thread.Sleep(10000);
        }
    }

    abstract class Shape
    {
        public abstract double calculatearea();
        public abstract bool Isvalid();
    }


    class Rectangle : Shape
    {
        public double Width { get; set; }
        public double Height { get; set; }

        public Rectangle(double width, double height)
        {
            Width = width;
            Height = height;
        }

        public override double calculatearea()
        {
            return Width * Height;
        }

        public override bool Isvalid()
        {
            return Width > 0 && Height > 0;
        }
    }

    class Circle : Shape
    {
        public double r { get; set; }


        public Circle(double radius)
        {
            r = radius;
        }

        public override double calculatearea()
        {
            return Math.PI * r * r;
        }

        public override bool Isvalid()
        {
            return r > 0;
        }
    }

    class Square : Shape
    {
        public double side { get; set; }


        public Square(double s)
        {
            side = s;
        }

        public override double calculatearea()
        {
            return side * side;
        }

        public override bool Isvalid()
        {
            return side > 0;
        }
    }
}
